#include <iostream>
#include <glut.h>   
#include <math.h>

static float angleIncrement = 1;

float hAng = 0;
float vAng = 0;

void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);			// Clear the color 
	glShadeModel(GL_FLAT);						// Set the shading model to GL_FLAT
	//glEnable(GL_LINE_SMOOTH);
	//glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);		// Set Line Antialiasing

	// Set initial camera position
	glLoadIdentity();
	gluLookAt(0,0,5,0,0,0,0,1,0);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glRotatef(hAng, 0,1,0);
	glRotatef(vAng, 1,0,0);

	//glTranslatef(0,0,-10);

	glColor3f (0.0, 1.0, 0.0);
	glutWireTeapot(1.0);

	glutSwapBuffers();
}

/*
Fir in height, not in width
*/
void reshape(int w, int h)
{
	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;
	float ratio = 1.0* w / h;

	// Use the Projection Matrix
	glMatrixMode(GL_PROJECTION);

        // Reset Matrix
	glLoadIdentity();

	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45,ratio,1,1000);

	// Get Back to the Modelview
	glMatrixMode(GL_MODELVIEW);
}

void processNormalKeys(unsigned char key, int x, int y)
{
	if (key == 27) // escape
		exit(0);
}

void processSpecialKeys(int key, int x, int y)
{
	switch(key)
	{
		case GLUT_KEY_LEFT :
			std::cout << "left" << std::endl;
			hAng = -angleIncrement;
			break;
		case GLUT_KEY_RIGHT :
			std::cout << "right" << std::endl;
			hAng = angleIncrement;
			break;
		case GLUT_KEY_UP :
			std::cout << "up" << std::endl;
			vAng = -angleIncrement;
			break;
		case GLUT_KEY_DOWN :
			std::cout << "down" << std::endl;
			vAng = angleIncrement;
			break;
	}

	glutPostRedisplay();
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(600,600);
	glutCreateWindow("OpenGL 3D Navigation Program");

	init();

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(processNormalKeys);
	glutSpecialFunc(processSpecialKeys);
	glutMainLoop();
}